package com.cgbl.cg.vm.home

import android.app.Application
import androidx.databinding.ObservableField
import api.CourseServiceFactory
import api.ProjectServiceFactory
import api.ResourceServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.*
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent
import me.goldze.mvvmhabit.http.BaseResponse

class RecommendMasterCourseFragmentViewModel(application: Application) :
    GLBaseViewModel(application) {
    val type = ObservableField(AppConstants.BundleValue.MASTER_COURSE)
    val tabType = ObservableField(RecommendMasterCourseViewModel.TAB_COURSE)
    var page = 1
    val uc = UiChangeEvent()
    var id = ""

    class UiChangeEvent {
        val refreshEvent = SingleLiveEvent<Int>()
        val resourceEvent = SingleLiveEvent<List<ResourceBean>>()
        val courseEvent = SingleLiveEvent<List<CourseBean>>()
        val projectEvent = SingleLiveEvent<List<ProjectBean>>()
    }

    fun getData() {
        when (type.get()) {
            AppConstants.BundleValue.MASTER_COURSE -> {
                //大师课程
                getCoursePage()
            }
            AppConstants.BundleValue.CLASS_CASE -> {
                //经典案例
                casePage()
            }
            AppConstants.BundleValue.POPULAR_RESOURCES -> {
                //热门资源
                getResource()
            }
            else -> {
                //VIP
                if (tabType.get() == RecommendMasterCourseViewModel.TAB_COURSE) {
                    getCoursePage()
                } else {
                    getResource()
                }
            }
        }
    }

    fun refresh() {
        page = 1
        getData()
    }

    fun load() {
        page++
        getData()
    }

    fun getCoursePage() {
        val map = hashMapOf(
            "pageNo" to page,
            "pageSize" to 10,
            "categoryId" to id
        )
        when (type.get()) {
            AppConstants.BundleValue.MASTER_COURSE -> {
                map["classRecommend"] = 4
            }
            else -> {
                map["isFree"] = 2
            }
        }

        CourseServiceFactory.page(
            map as Map<String, Any>?
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<CourseBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<CourseBean>>) {
                uc.courseEvent.postValue(t.data.list)
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < page * 10) {
                        0
                    } else {
                        1
                    }
                )
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(
                    0
                )
            }
        })
    }

    fun casePage() {
        ProjectServiceFactory.casePage(
            mapOf(
                "pageNo" to page,
                "pageSize" to 10,
                "categoryId" to id
            )
        )
            .subscribe(object :
                AbstractViewModelSubscriber<BaseResponse<PageListBean<ProjectBean>>>(this) {
                override fun onNext(t: BaseResponse<PageListBean<ProjectBean>>) {
                    uc.projectEvent.postValue(t.data.list)
                    uc.refreshEvent.postValue(
                        if (t.data.total.toInt() < page * 10) {
                            0
                        } else {
                            1
                        }
                    )
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    uc.refreshEvent.postValue(
                        0
                    )
                }
            })
    }

    fun getResource() {
        val map = hashMapOf(
            "pageNo" to page,
            "pageSize" to 10,
            "categoryId" to id
        )
        when (type.get()) {
            AppConstants.BundleValue.POPULAR_RESOURCES -> {
                map["sort"] = 1
            }
            else -> {
                map["isFree"] = 2
            }
        }
        ResourceServiceFactory.page(
            map as Map<String, Any>?
        ).subscribe(object :
            AbstractViewModelSubscriber<BaseResponse<PageListBean<ResourceBean>>>(this) {
            override fun onNext(t: BaseResponse<PageListBean<ResourceBean>>) {
                uc.resourceEvent.postValue(t.data.list)
                uc.refreshEvent.postValue(
                    if (t.data.total.toInt() < page * 10) {
                        0
                    } else {
                        1
                    }
                )
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                uc.refreshEvent.postValue(
                    0
                )
            }
        })
    }
}